package fr.oz.init 
{
	import flash.desktop.NativeApplication;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.text.Font;
	import fr.digitas.flowearth.command.Batcher;
	import fr.digitas.flowearth.conf.Conf;
	import fr.digitas.flowearth.net.BatchLoaderItem;
	import fr.digitas.flowearth.text.styles.styleManager;
	import fr.oz.core.fonts.FontList;
	import fr.oz.core.fonts.GetFonts;
	
	/**
	 * Classe d'initialisation
	 * @author Fazermokeur
	 */
	public class Launcher extends Sprite
	{
		
		public function Launcher() 
		{
			var loader:Loader = new Loader();
			Conf.addEventListener(Event.COMPLETE, onConfLoaded);
			Conf.addEventListener(ProgressEvent.PROGRESS, onconfProgress);
			Conf.addEventListener(ErrorEvent.ERROR, onConfError);
			Conf.loadXml(new URLRequest("xml/conf.xml"));
		}
		
		private function onConfError(e:ErrorEvent):void 
		{
			trace("Error de chargement");
		}
		
		private function onconfProgress(e:ProgressEvent):void 
		{
			trace(e.bytesLoaded + "/" + e.bytesTotal);
		}
		
		private function onConfLoaded(e:Event):void 
		{
			styleManager.addCss(Conf.getString("style"));
			loadFonts();
		}
		
		private function loadFonts():void
		{
			var xmlFonts:XML = Conf.getDatas("fontToLoad");
			
			FontList.getFonts(xmlFonts);
			
			var context:LoaderContext = new LoaderContext(true, ApplicationDomain.currentDomain);
			
			var ba:Batcher = new Batcher();
			
			var nbFonts:int = xmlFonts.children().length();
			trace(nbFonts +" fonts to load");
			for (var i:int = 0; i < nbFonts; i++) 
			{
				var req:URLRequest = new URLRequest(xmlFonts.font[i]);
				var bItem:BatchLoaderItem = new BatchLoaderItem(req, context);
				
				ba.addItem(bItem);
			}
			
			ba.addEventListener(ProgressEvent.PROGRESS, handleProgress);
			ba.addEventListener(Event.COMPLETE, onLoadFont);
			ba.addEventListener(ErrorEvent.ERROR, onErrorFont);
			
			ba.start();
		}
		
		private function onErrorFont(e:ErrorEvent):void 
		{
			e.currentTarget.removeEventListener(ProgressEvent.PROGRESS, handleProgress);
			e.currentTarget.removeEventListener(Event.COMPLETE, onLoadFont);
			e.currentTarget.removeEventListener(ErrorEvent.ERROR, onErrorFont);
			
			trace("Fonts loading error : " + e.text);
			
			NativeApplication.nativeApplication.exit(0);
		}
		
		private function onLoadFont(e:Event):void 
		{
			e.currentTarget.removeEventListener(ProgressEvent.PROGRESS, handleProgress);
			e.currentTarget.removeEventListener(Event.COMPLETE, onLoadFont);
			e.currentTarget.removeEventListener(ErrorEvent.ERROR, onErrorFont);
			
			var embeddedFonts:Array = Font.enumerateFonts(false);
			trace("embeddedFonts.length : " + embeddedFonts.length);
			for (var j:uint = 0; j < embeddedFonts.length; j++) {
				var item:Font = embeddedFonts[j];	
				GetFonts.buildFonts(item);
			}
			
			trace("3: conf : " + Conf.getDatas("wakeUpProfile"));
			loadSite();
		}
		
		private function handleProgress(e:ProgressEvent):void 
		{
			
		}
		
		private function loadSite():void
		{
			var main:Main = new Main();
			addChild(main);
		}
		
	}

}